package com.demo.mangyThread.threadException.csdn;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.TimeUnit;

/**
 * @Author: lkz
 * @Title: ExceptionInChildThread4
 * @Description:
 *  全局:
 * Thread.setDefaultUncaughtExceptionHandler(new CustomThreadUncaughtExceptionHandler());
     * 通过调用Thread的静态方法setDefaultUncaughtExceptionHandler()，
     * 设置Thread的静态属性defaultUncaughtExceptionHandler.为我们自定义的异常处理器。
 * @Date: 2023/3/28 11:27
 */
@Slf4j
public class ExceptionInChildThread4 implements Runnable{

    @Override
    public void run() {
        throw new RuntimeException("子线程发生了异常...");
    }

    /**
     * 模拟子线程发生异常
     *
     * @throws InterruptedException
     */
    private static void exceptionThread() throws InterruptedException {
        new Thread(new ExceptionInChildThread4()).start();
        TimeUnit.MILLISECONDS.sleep(200L);
        new Thread(new ExceptionInChildThread4()).start();
        TimeUnit.MILLISECONDS.sleep(200L);
        new Thread(new ExceptionInChildThread4()).start();
        TimeUnit.MILLISECONDS.sleep(200L);
        new Thread(new ExceptionInChildThread4()).start();
        TimeUnit.MILLISECONDS.sleep(200L);
    }

    public static void main(String[] args) throws InterruptedException {
        //设置全局的线程异常处理器
        Thread.setDefaultUncaughtExceptionHandler(new CustomThreadUncaughtExceptionHandler());
        exceptionThread();
    }


}
